package com.yjm.test.Spring5.ch6.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.yjm.test.Spring5.ch6.dao.SingerDao;
import com.yjm.test.Spring5.ch6.entities.Singer;

@Repository
public class JdbcSingerDao implements SingerDao, InitializingBean {
	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource);
		
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Singer> findAll() {
		String sql =  "select * from SINGER";
		return jdbcTemplate.query(sql, (rs, rowNum) -> {
			Singer singer = new Singer();
			singer.setId(rs.getLong("id"));
			singer.setFirstName(rs.getString("first_name"));
			singer.setLastName(rs.getString("last_name"));
			singer.setBirthDate(rs.getDate("birth_date"));
			return singer;
		});
	}

	@Override
	public List<Singer> findByFirstName(String firstName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String findLastNameById(Long id) {
		return jdbcTemplate.queryForObject("select last_name from SINGER where id=?", new Object[]{id}, String.class);
	}

	@Override
	public String findFirstNameById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insert(Singer singer) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean update(Singer singer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Long singerId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Singer> findAllWithDetail() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertWithDetail(Singer singer) {
		// TODO Auto-generated method stub

	}
	
	@Configuration
	@PropertySource("classpath:db/jdbc.properties")
	@ComponentScan(basePackages="com.yjm.test.Spring5.ch6.dao.impl")
	public static class AppConfig {
		@Value("${jdbc.driverClassName}")
		private String driverClassName;
		@Value("${jdbc.url}")
		private String url;
		@Value("${jdbc.username}")
		private String username;
		@Value("${jdbc.password}")
		private String password;
		
		@Lazy
		@Bean
		public DataSource dataSource() {
			BasicDataSource dataSource = new BasicDataSource();
			dataSource.setDriverClassName(driverClassName);
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
			return dataSource;
		}
	}

	public static void main(String[] args) {
		GenericApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
		SingerDao singerDao = ctx.getBean("jdbcSingerDao", SingerDao.class);
		
		System.out.println("id:" + singerDao.findLastNameById((long) 1));
		List<Singer> singers = singerDao.findAll();
		for (Singer singer:singers) {
			System.out.println(singer.toString());
		}
		
		ctx.close();
	}

}
